var express = require('express');
var router = express.Router();
var http = require('http');
var fs = require('fs');



require('mongoose-pagination');
var UserModel = require('../SEmodel/user');

/* GET Register page. */
router.get('/register', function (req, res, next) {
    res.render('web/register', {
        layout: 'columlo',
        title: '欢迎注册deer'
    });
});

router.post('/register', function (req, res) {

    //判断两次输入的密码是否一致
    var pwd = req.body.password;
    var repwd = req.body.repassword;

    var errors = [];
    if (repwd != pwd) {
        errors.push({field: password, message: '您两次输入的密码不一致,请重新输入'});
        return res.json(errors);
    }

    var Users = {
        email: req.body.email,
        password: pwd,
        createtime: new Date()
    }

    LOG('[trace] post register,users=' + JSON.stringify(Users));

    //验证邮箱是否存在,如果不存在,创建新的账户

    UserModel.create(Users, function (err, data) {
        if (err) {
            LOG('[error] user create faild,err=' + JSON.stringify(err));

            errors = Main.errorHelper(err);
            return res.json(errors);
        }
        else {
            LOG('[trace] user create success,users=' + JSON.stringify(Users));

            if (req.body.ajax) {
                req.session.Users = Users;
                var data = {err: 0, message: '注册成功', gourl: '/', users: Users};
                LOG('[trace] res =' + JSON.stringify(data));

                return res.json(data);
            }
        }

    });

});


/* GET Login Page */

router.get('/login', function (req, res) {
    LOG('[trace] get  user login');

    res.render('web/login', {
        layout: 'columlo',
        title: '欢迎登录deer'
    });

});


router.post('/login', function (req, res) {

    var Email = req.body.email;
    var Password = req.body.password;
    LOG('[trace] post user =' + JSON.stringify(req.body));



    if (Email == '' || Password == '') {
        res.locals.error = '邮箱，密码不能为空';
        return res.render('web/login', {layout: 'columlo', title: '欢迎登录deer'});
    }


    //var p = Email.match(/\w+@\w{1,10}\.\w{2,10}/g);
    ////判断邮箱格式是否正确
    //LOG('[trace] user match email,p=' + JSON.stringify(p));
    //if (!p) {
    //
    //    res.locals.error = '邮箱格式错误';
    //    return res.render('web/login', {layout: 'columlo', title: '欢迎登录deer'});
    //}

    //查询数据库邮箱是否存在,判断两次输入的密码是否正确
    LOG('[trace] login post user =' + JSON.stringify(req.body));

    UserModel.find({email: Email, password: Password}, function (err, data) {
        if (data.length > 0) {
            LOG('[trace] login post user =' + JSON.stringify(data));
            req.session.Users = data[0];
            return res.redirect('/');
        }
        else{

            res.locals.error = "用户名或密码错误";
            return res.render('web/login', {layout: 'columlo', title: '欢迎登录deer'});
        }
    });

});




/* GET Index Page */
router.get('/', function(req, res, next){
    var user = req.session.Users || {email:''};

    LOG('[trace] user session='+JSON.stringify(user));

    res.render('web/index', {title: '欢迎来到deer',user:user});
});


/* GET List Page */
router.get('/list', function(req, res, next){
    var user = req.session.Users || {email:''};
    
    res.render('web/list', {title: '欢迎来到deer', user:user});
});



//用户登出

router.get('/logout', function(req, res, next){
    req.session.Users = "";
    return res.redirect('/login');
});



module.exports = router;



